#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Dylan"


from . import home
from flask import render_template, redirect, request, flash
from flask import url_for, current_app
from myApp.home.form import RegistForm, LoginForm
from myApp.home import models
from myApp import db
import re
import hashlib


@home.route("/", methods=['GET', 'POST'])
def base():
    return render_template('login.html')


@home.route("/index", methods=['GET', 'POST'])
def index():
    return render_template('index.html')


@home.route("/play", methods=['GET', 'POST'])
def play():
    return render_template('play.html')


@home.route("/search", methods=['GET', 'POST'])
def search():
    return render_template('search.html')


@home.route("/user", methods=['GET', 'POST'])
def user():
    return render_template('user.html')


@home.route("/pwd", methods=['GET', 'POST'])
def pwd():
    return render_template('pwd.html')


@home.route("/comments", methods=['GET', 'POST'])
def comments():
    return render_template('comments.html')


@home.route("/loginlog", methods=['GET', 'POST'])
def loginlog():
    return render_template('loginlog.html')


@home.route("/logout", methods=['GET', 'POST'])
def logout():
    return render_template('login.html')


@home.route("/moviecol", methods=['GET', 'POST'])
def moviecol():
    return render_template('moviecol.html')


@home.route("/login", methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        account = request.form.get('contact', None)
        FillPwd = request.form.get('password', None)
        FillPwd_Md5 = hashlib.md5(FillPwd.encode('utf8'))     # md5加密
        password_Md5 = FillPwd_Md5.hexdigest()
        try:
            if '@' in account:
                search_email = models.user.query.filter_by(email=account).first()
                if search_email is not None and search_email.pwd == password_Md5:
                    return render_template('index.html')
                else:
                    return render_template("login.html", contact_error="用户名或密码不正确！")
            elif re.match('^\d', account):   #首位是否为数字
                search_phone = models.user.query.filter_by(phone=account).first()
                if search_phone is not None and search_phone.pwd == password_Md5:
                    return render_template('index.html')
                else:
                    return render_template("login.html", contact_error="用户名或密码不正确！")
            else:
                search_usr = models.user.query.filter_by(nickname=account).first()
                # print(type(search_usr))
                # print(search_usr)
                if search_usr is not None and search_usr.pwd == password_Md5:
                    return render_template('index.html')
                else:
                    return render_template("login.html", contact_error="用户名或密码不正确！")
        except Exception:
            return render_template("login.html", contact_error="Error:500,请刷新后重新登录")

    return render_template("login.html")


@home.route("/regist", methods=['POST', 'GET'])
def regist():
    if request.method == 'POST':
        nickname = request.form.get('nickname', None)
        email = request.form.get('email', None)
        phone = request.form.get('phone', None)
        password = request.form.get('password', None)
        pwd_temp = hashlib.md5(request.form['password'].encode('utf8'))     # md5加密
        password = pwd_temp.hexdigest()
        info = models.user(email=email, nickname=nickname, phone=phone, pwd=password)
        db.session.add(info)
        db.session.commit()
        return render_template("login.html")
    return render_template('regist.html')
